package com.ostrich.cloud.ostrichGoodsService.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ostrich.cloud.common.exceptions.OstrichException;
import com.ostrich.cloud.common.result.PageResult;
import com.ostrich.cloud.ostrichGoodsSercice.model.Dto.GrounpGoodsDto;
import com.ostrich.cloud.ostrichGoodsService.common.exception.OstrichGoodsException;
import com.ostrich.cloud.ostrichGoodsService.common.exception.OstrichGoodsExceptionCode;
import com.ostrich.cloud.ostrichGoodsService.common.status.GrounpGoodsStatus;
import com.ostrich.cloud.ostrichGoodsService.common.status.SecKillStatus;
import com.ostrich.cloud.ostrichGoodsService.infra.model.GrounpGoods;
import com.ostrich.cloud.ostrichGoodsService.infra.mapper.GrounpGoodsMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ostrich.cloud.ostrichGoodsService.infra.model.SecKill;
import com.ostrich.cloud.ostrichGoodsService.service.GrounpGoodsService;
import com.ostrich.cloud.ostrichGoodsService.service.param.AddGrounpParam;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 拼团商品表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2022-11-03
 */
@Service
public class GrounpGoodsServiceImpl extends ServiceImpl<GrounpGoodsMapper, GrounpGoods> implements GrounpGoodsService {


    @Resource
    private GrounpGoodsMapper grounpGoodsMapper;

    @Override
    @Transactional
    public GrounpGoods addGrounpGoods(AddGrounpParam addGrounpParam) {

        GrounpGoods grounpGoods = BeanUtil.copyProperties(addGrounpParam, GrounpGoods.class);

        grounpGoods.setStatus(GrounpGoodsStatus.STAY);
        grounpGoods.setAddTime(new Date());
        grounpGoods.setSale(0);
        int insert = grounpGoodsMapper.insert(grounpGoods);
        return grounpGoods;
    }

    @Override
    public PageResult<GrounpGoodsDto> selectGrounpGoods(Integer current, Integer size) {

        QueryWrapper<GrounpGoods> wrapper = new QueryWrapper<>();

        Page<GrounpGoods> grounpGoodsPage = grounpGoodsMapper.selectPage(new Page<>(current, size), wrapper);

        PageResult<GrounpGoodsDto> result=null;

        if(grounpGoodsPage!=null&&grounpGoodsPage.getRecords().size()!=0){
            List<GrounpGoodsDto> grounpGoodsDtos = BeanUtil.copyToList(grounpGoodsPage.getRecords(), GrounpGoodsDto.class);
            result=new PageResult<>();
            result.setTotal(grounpGoodsPage.getTotal());
            result.setTotalPage(grounpGoodsPage.getPages());
            result.setPageSize(grounpGoodsPage.getSize());
            result.setPage(grounpGoodsPage.getCurrent());
            result.setElements(grounpGoodsDtos);
        }

        return result;

    }

    @XxlJob("startGrounpGoods")
    public void startGrounpGoods(){
        String jobParam = XxlJobHelper.getJobParam();
        int update = grounpGoodsMapper.update(null, new UpdateWrapper<GrounpGoods>().eq("ground_id", jobParam)
                .set("status",GrounpGoodsStatus.BEGIN));
        if (update==0){
            throw new OstrichGoodsException(OstrichGoodsExceptionCode.TYPE_DEL_DB_EXCEPTION);
        }
    }

    @XxlJob("endGrounpGoods")
    public void endGrounpGoods(){
        String jobParam = XxlJobHelper.getJobParam();
        int update = grounpGoodsMapper.update(null, new UpdateWrapper<GrounpGoods>().eq("ground_id", jobParam)
                .set("status",GrounpGoodsStatus.END));
        if (update==0){
            throw new OstrichGoodsException(OstrichGoodsExceptionCode.TYPE_DEL_DB_EXCEPTION);
        }
    }


}
